package spider;

import org.openqa.selenium.Cookie;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.downloader.Downloader;
import us.codecraft.webmagic.selector.PlainText;
import util.AgentUtil;

import java.util.Optional;
import java.util.function.BiConsumer;

/**
 * @author zhanglinfeng
 * @date create in 2022/10/21 16:14
 */
public class MyDownloader implements Downloader {
    protected Logger LOGGER = LoggerFactory.getLogger(MyDownloader.class);

    private final RemoteWebDriver driver;
    private BiConsumer<RemoteWebDriver, String> mouseBehavior;

    public MyDownloader() {
        //第一个参数是使用哪种浏览器驱动，第二个参数是浏览器驱动的地址 TODO 驱动地址
        System.setProperty("webdriver.chrome.driver", "");
        // 创建浏览器参数对象
        ChromeOptions chromeOptions = new ChromeOptions();
        // 设置为 无界面浏览器 模式,若是不想看到浏览器打开，就可以配置此项
        chromeOptions.addArguments("--headless");
        chromeOptions.addArguments("--no-sandbox");
        // 谷歌文档提到需要加上这个属性来规避bug
        chromeOptions.addArguments("--disable-gpu");
        // 全屏展示
        chromeOptions.addArguments("--start-maximized");
        // 不加载图片
        chromeOptions.addArguments("blink-settings=imagesEnabled=false");
        chromeOptions.addArguments("User-Agent=" + AgentUtil.get());
        driver = new ChromeDriver(chromeOptions);
    }

    @Override
    public Page download(Request request, Task task) {
        driver.get(request.getUrl());
        Page page = new Page();
        try {
            Thread.sleep(3000);
            Site site = task.getSite();
            if (site.getCookies() != null) {
                site.getCookies().forEach((key, value) -> driver.manage().addCookie(new Cookie(key, value)));
                driver.get(request.getUrl());
            }
            Thread.sleep(3000);
            //需要滚动到页面的底部,获取完整的数据
            driver.executeScript("window.scrollTo(0,document.body.scrollHeight)");
            Thread.sleep(2000);
            //获取页面，打包成Page对象，传给PageProcessor 实现类
            String content = driver.getPageSource();
            //模拟鼠标行为
            if (mouseBehavior != null) {
                mouseBehavior.accept(driver, request.getUrl());
            }
            page.setRawText(content);
            page.setUrl(new PlainText(request.getUrl()));
            page.setRequest(new Request(request.getUrl()));
            page.setDownloadSuccess(true);
            LOGGER.info("downloading page success :{} ", request.getUrl());
        } catch (InterruptedException e) {
            page.setDownloadSuccess(false);
        }
        return page;
    }

    @Override
    public void setThread(int threadNum) {

    }

    public void quitWebDriver() {
        Optional.ofNullable(driver).ifPresent(RemoteWebDriver::quit);
    }

    public void setMouseBehavior(BiConsumer<RemoteWebDriver, String> mouseBehavior) {
        this.mouseBehavior = mouseBehavior;
    }
}
